﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;

using ISS.CaseStudy.DAL;
using ISS.CaseStudy.Entities;

namespace ISS.CaseStudy.BLL
{
    public class UserInformationBLL
    {
        public static DataSet GetAll()
        {
            return UserInfoDLL.GetAllInfos();
        }

        public static UserInfo GetInfo(string id)
        {
            return UserInfoDLL.GetInfo(id);
        }

        public static bool Update(UserInfo userInfo, out string message)
        {
            message = string.Empty;

            DataSet dataSet = UserInfoDLL.GetAllInfos();
            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                if ((userInfo.Name.Equals(row["Name"].ToString())) && 
                    (!userInfo.Id.Equals(row["Id"].ToString())))
                {
                    message = "The user already existed!";
                    return false;
                }
            }

            return UserInfoDLL.Update(userInfo);
        }

        public static bool Insert(UserInfo userInfo, out string message)
        {
            message = string.Empty;

            DataSet dataSet = UserInfoDLL.GetAllInfos();
            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                if (userInfo.Name.Equals(row["Name"].ToString()))
                {
                    message = "The user already existed!";
                    return false;
                }
            }

            return UserInfoDLL.Insert(userInfo);
        }

        public static bool Delete(string id)
        {
            return UserInfoDLL.Delete(id);
        }
    }
}
